package com.shopping.mall.core.module.specialValue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.specialValue.entity.SpecialValue;
import com.shopping.mall.core.module.specialValue.service.SpecialValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "特殊值")
@RestController
@RequestMapping("/specialValue")
public class SpecialValueController {
    @Autowired
    private SpecialValueService specialValueService;

    @ApiOperation("分页查询")
//    @GetMapping("/page")
    public Result<IPage<SpecialValue>> getSpecialValuePage(
        @RequestParam(name = "current", defaultValue = "1") Integer current,
        @RequestParam(name = "size", defaultValue = "10") Integer size,
        SpecialValue specialValue) {
        IPage<SpecialValue> specialValuePage=specialValueService.getSpecialValuePage(current,size,specialValue);
        return Result.OK(specialValuePage);
    }

    @UnLogin
    @ApiOperation("修改")
    @PutMapping
    public Result<SpecialValue> update(@Validated(SpecialValue.update.class) @RequestBody SpecialValue specialValue) {
        specialValueService.update(specialValue,new UpdateWrapper<>());
        return Result.OK();
    }

    @ApiOperation("新增")
//    @PostMapping
    public Result<Object> add(@Validated(SpecialValue.insert.class) @RequestBody SpecialValue specialValue) {
        specialValueService.save(specialValue);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
//    @GetMapping("/id")
    public Result<SpecialValue> getById(@RequestParam String id){
        SpecialValue specialValue=specialValueService.getById(id);
        return Result.OK(specialValue);
    }

    @UnLogin
    @ApiOperation("查询")
    @GetMapping
    public Result<SpecialValue> getAll(){
        SpecialValue specialValue = specialValueService.getOne(new QueryWrapper<>());
        return Result.OK(specialValue);
    }

    @ApiOperation("通过id删除")
//    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id){
        return specialValueService.deleteById(id);
    }

    @ApiOperation("批量删除")
//    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids){
        return specialValueService.batchDelete(ids);
    }
}
